package com.seecen.controller;

import com.seecen.pojo.entity.Agency;
import com.seecen.service.AgencyService;
import com.seecen.utils.Result;
import com.seecen.utils.ResultCode;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * FIND YOUR PURPOSE
 *
 * @author: CYZ
 * @date: 2025/10/20/12:25
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/api")
@CrossOrigin
public class AgencyController {

    @Autowired
    private AgencyService agencyService;

    /**
     * 查询旅行社信息（需验证码）
     * @param agencyName 旅行社名称（模糊查询）
     * @param captcha 用户输入的验证码
     * @param session HTTP会话，用于获取正确的验证码
     * @return 旅行社列表
     */
    @PostMapping("/agency")
    public Result getAgencies(@RequestParam("agencyName") String agencyName,
                              @RequestParam("captcha") String captcha,
                              HttpSession session) {
        log.info("查询旅行社信息，名称：{}，验证码：{}", agencyName, captcha);

        // 从session中获取正确的验证码
        String correctCaptcha = (String) session.getAttribute("captcha");

        // 核对 验证码（忽略大小写）
        if (correctCaptcha == null || !captcha.equalsIgnoreCase(correctCaptcha)) {
            return Result.build(null, ResultCode.CAPTCHA_ERROR);
        }

        List<Agency> agencies = agencyService.getAgenciesByName(agencyName);
        return Result.ok(agencies);
    }
}